﻿#include <windows.h>
#include <iostream>
#include <thread>
#include <atomic>

std::atomic<bool> g_running(false);

void sendKey(WORD vk) 
{
    INPUT input = {0};
    input.type = INPUT_KEYBOARD;
    input.ki.wVk = vk;
    SendInput(1, &input, sizeof(INPUT));

    // Key up
    input.ki.dwFlags = KEYEVENTF_KEYUP;
    SendInput(1, &input, sizeof(INPUT));
}

void autoFire() 
{
    while (g_running) 
    {
        sendKey(VK_SPACE);   // 连发空格键
        std::this_thread::sleep_for(std::chrono::milliseconds(100)); // 间隔 100ms
    }
}

int main() 
{
    std::cout << "按下 J 键开始连发，松开 J 键停止连发。按 ESC 退出。\n";

    while (true) 
    {
        // 检查 J 键是否按下
        if (GetAsyncKeyState('J') & 0x8000) 
        { 
            if (!g_running) 
            {
                g_running = true;
                std::thread(autoFire).detach();
                std::cout << "连发启动\n";
            }
        } 
        else 
        {
            if (g_running) 
            {
                g_running = false;
                std::cout << "连发停止\n";
            }
        }

        if (GetAsyncKeyState(VK_ESCAPE) & 1) 
        {
            break;
        }

        Sleep(20); // 避免CPU占用过高
    }

    return 0;
}
